package cn.lhq.service;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.lhq.bean.Activity;
import cn.lhq.bean.ActivityAndVotes;
import cn.lhq.bean.CreateActivityForm;
import cn.lhq.bean.ModifyActivityForm;
import cn.lhq.bean.Vote;
import cn.lhq.bean.VoteResult;
import cn.lhq.dao.ActivityDao;
import cn.lhq.dao.VoteDao;
@Service
public class ActivityServiceImpl implements ActivityService{
	@Resource
	private ActivityDao activityDao;
	@Resource
	private VoteDao voteDao;
	
	public VoteResult createActivity(CreateActivityForm form)
			throws Exception {

		String title = form.getTitle();
		String begin_at = form.getBegin_at();
		String end_at = form.getEnd_at();
		int role = form.getRole();
		int creator = form.getCreator();
		
		VoteResult result = new VoteResult();
		Activity activity = activityDao.findByActivityName(title);
		if(activity != null){
			result.setStatus(1);
			result.setMsg("已有同名活动");
			return result;
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date begin_date = sdf.parse(begin_at);
		Date end_date = sdf.parse(end_at);
		Activity activity2 = new Activity();
		activity2.setTitle(title);
		activity2.setBegin_at(begin_date);
		activity2.setEnd_at(end_date);
		activity2.setState(role);
		activity2.setCreator(creator);
		activityDao.save(activity2);
		activity2 = activityDao.findByActivityName(title);
		int id = activity2.getId();
		
		String[] votes = form.getVotes();
		for(int i=0; i<votes.length; i++){
			Vote vote = new Vote();
			vote.setActivity_id(id);
			vote.setCount(0);
			vote.setTitle(votes[i]);
			vote.setVote_key(String.valueOf(i));
			voteDao.save(vote);
		}
		
		result.setStatus(0);
		result.setMsg("活动创建成功");
		
		return result;
	}

	public VoteResult modifyActivity(ModifyActivityForm form) throws Exception {
		VoteResult result = new VoteResult();
		String title = form.getTitle();
		String begin_at = form.getBegin_at();
		String end_at = form.getEnd_at();
		Integer state = form.getState();
		String[] votes = form.getVotes();
		Activity activity2 = activityDao.findByActivityName(title);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date begin_date = sdf.parse(begin_at);
		Date end_date = sdf.parse(end_at);
		activity2.setTitle(title);
		activity2.setBegin_at(begin_date);
		activity2.setEnd_at(end_date);
		activity2.setState(state);
		activityDao.update(activity2);
		int id = activity2.getId();
		voteDao.deleteByActivityId(id);

		for(int i=0; i<votes.length; i++){
			Vote vote = new Vote();
			vote.setActivity_id(id);
			vote.setCount(0);
			vote.setTitle(votes[i]);
			vote.setVote_key(String.valueOf(i));
			voteDao.save(vote);
		}
		result.setStatus(0);
		result.setMsg("修改成功");
		
		return result;
	}	

	public VoteResult findAll() throws Exception {
		VoteResult result = new VoteResult();
		List<Activity> list = activityDao.findAll();
		if(list == null){
			result.setStatus(1);
			result.setMsg("没有记录");
		}
		result.setStatus(0);
		result.setMsg("查询成功");
		result.setData(list);
		return result;
	}

	public VoteResult delete(int activityId) throws Exception {
		VoteResult result = new VoteResult();
//		Activity activity = hibernateActivityDao.findByActivityId(activityId);
//		hibernateActivityDao.delete(activity);
		activityDao.deleteById(activityId);
		voteDao.deleteByActivityId(activityId);
		result.setStatus(0);
		result.setMsg("活动删除成功");
		return result;
	}

	public VoteResult findByCreatorId(int creatorId) throws Exception {
		VoteResult result = new VoteResult();
		List<Activity> list = activityDao.findByCreatorId(creatorId);
		if(list == null){
			result.setStatus(1);
			result.setMsg("没有记录");
		}
		result.setStatus(0);
		result.setMsg("查询成功");
		result.setData(list);
		return result;
	}

	public VoteResult findByActivityId(int activityId) throws Exception {
		VoteResult result = new VoteResult();
		Activity activity = activityDao.findByActivityId(activityId);
		if(activity == null){
			result.setStatus(1);
			result.setMsg("没有记录");
		}
		List<Vote> votes = voteDao.findByActivityId(activityId);
		ActivityAndVotes aav = new ActivityAndVotes();
		aav.setActivity(activity);
		aav.setVotes(votes);
		
		result.setStatus(0);
		result.setMsg("查询成功");
		result.setData(aav);
		return result;
	}


	
}
